package com.czq.gulimail.gulimailproduct.controller;

import com.czq.common.utils.PageUtils;
import com.czq.common.utils.R;
import com.czq.gulimail.gulimailproduct.entity.AttrEntity;
import com.czq.gulimail.gulimailproduct.entity.AttrGroupEntity;
import com.czq.gulimail.gulimailproduct.service.AttrGroupService;
import com.czq.gulimail.gulimailproduct.vo.AttrGroupRelationVO;
import com.czq.gulimail.gulimailproduct.vo.resp.AttrGroupWithAttrsResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author czq
 * @email 1807099124@qq.com
 * @date 2024-09-23 19:03:06
 */
@RestController
@RequestMapping("gulimailproduct/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;


    @RequestMapping("/{catelogId}/withattr")
    public R withattr(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsResp> attrGroupWithAttrsResp=attrGroupService.withattr(catelogId);
        return R.ok().put("data", attrGroupWithAttrsResp);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelog_id}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelog_id") Long catelog_id) {


        PageUtils page = attrGroupService.queryPage(params, catelog_id);

        return R.ok().put("page", page);
    }

    /**
     * 获取当前属性分组下管理的属性
     */
    @RequestMapping("/list/relation")
    public R relation(@RequestParam Long attrGroupId) {

        List<AttrEntity> res=attrGroupService.getAttrRelation(attrGroupId);

        return R.ok().put("data", res);
    }

    /**
     * 获取当前属性分组下没有管理的属性
     */
    @RequestMapping("/list/not-relation/{catlogId}")
    public R notRelation(@PathVariable("catlogId") Long catlogId) {

        List<AttrEntity> res=attrGroupService.getAttrNotRelation(catlogId);

        return R.ok().put("data", res);
    }

    /**
     * 新增关联
     * @param vos
     * @param attrGroupId
     * @return
     */
    @RequestMapping("save/relation/{attrGroupId}")
    public R saveRelation(@RequestBody List<AttrGroupRelationVO> vos, @PathVariable("attrGroupId") Long attrGroupId) {
        attrGroupService.saveRelation(vos, attrGroupId);
        return R.ok();
    }


    /**
     * 删除当前属性分组下管理的属性
     */
    @RequestMapping("/delete/relation")
    public R deleteRelation(@RequestBody List<AttrGroupRelationVO> voList) {
        attrGroupService.deleteRelation(voList);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
